package com.yidianyanglao.common.controller;

import com.yidianyanglao.common.bean.Banner;
import com.yidianyanglao.common.bean.FunctionMenu;
import com.yidianyanglao.common.bean.PlatformData;
import com.yidianyanglao.common.mapper.CommonMapper;
import com.yidianyanglao.core.bean.MobileCode;
import com.yidianyanglao.core.bean.RetBean;
import com.yidianyanglao.core.enums.Code;
import com.yidianyanglao.core.exception.CustomException;
import com.yidianyanglao.core.util.FileUploadOss;
import com.yidianyanglao.core.util.Log;
import com.yidianyanglao.core.util.SendMsg;
import com.yidianyanglao.core.util.SmsUtil;
import com.yidianyanglao.user.bean.UserBean;
import com.yidianyanglao.user.mapper.UserMapper;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * Created by chenkelong on 2016/11/2.
 */

@Controller
public class CommonController {

    @Resource
    private CommonMapper commonMapper;

    @Resource
    private UserMapper userMapper;

    /**
     * 文件上传并返回上传地址
     * @param file
     * @return
     */
    @RequestMapping("/common/uploadFile.json")
    @ResponseBody
    public RetBean uploadFile(@RequestParam(value = "file", required = false) MultipartFile file){
        return FileUploadOss.uploadPic(file);
    }


    /**
     * 用户获取验证码的功能
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/common/getMobileCode.json")
    @ResponseBody
    public RetBean getMobileCode(HttpServletRequest request, HttpServletResponse response, String mobile) {

        if(StringUtils.isEmpty(mobile)){
            throw new CustomException(Code.ERROR, "电话号码不能为空");
        }
        if(mobile.length() != 11){
            throw new CustomException(Code.ERROR, "请正确输入手机号码！");
        }

        String checkMobile = commonMapper.chenkMobileCodeHas(mobile);
        if(!StringUtils.isEmpty(checkMobile)){
            if(Integer.parseInt(checkMobile) <= 40){
                throw new CustomException(Code.ERROR, "您有未验证的验证码，请稍后重试！");
            }
        }

        //生成验证码
        String VCode = RandomStringUtils.random(6, "0123456789");
        //String VCode = "123456";
        MobileCode mobileCode = new MobileCode(mobile,VCode);

        //SmsUtil.sendSms(mobile,"尊敬的用户，您当前操作的验证码为"+VCode+",五分钟内有效，请妥善保管您的验证码，如非本人操作请勿转告他人！【一点养老】");
        //阿里云短信通道
            SendMsg.sendMsg(VCode,mobile);
        try{
            commonMapper.addMobileCode(mobileCode);
        }catch (Exception e){
            Log.error("验证码插入数据库异常：",e);
            throw new CustomException(Code.ERROR, e.getMessage());
        }
        return new RetBean(Code.SUCCESS,"请求成功","");
    }


    /**
     *  验证用户是否存在接口
     * @param account
     * @return
     */
    @RequestMapping("/common/checkUserHas.json")
    @ResponseBody
    public RetBean checkUserHas(HttpServletResponse response,HttpServletRequest request, String account){

        if(StringUtils.isEmpty("account")){
            throw new CustomException(Code.ERROR,"账号不能为空");
        }

        Map<String,Object> map = commonMapper.checkUserHas(account);

        if(map==null){
            throw new CustomException(Code.ERROR,"用户信息不存在");
        }

        return new RetBean(Code.SUCCESS,"用户信息查询成功",map);

    }


    /**
     *  检查验证码是否成功
     * @param mobile,code
     * @return
     */
    @RequestMapping("/common/checkMobileCode.json")
    @ResponseBody
    public RetBean checkMobileCode(HttpServletResponse response,HttpServletRequest request, String mobile,String code){

        if(StringUtils.isEmpty("mobile")){
            throw new CustomException(Code.ERROR,"电话号码不能为空");
        }
        if(StringUtils.isEmpty("code")){
            throw new CustomException(Code.ERROR,"验证码不能为空");
        }

        MobileCode mobileCode = commonMapper.getMobileCode(mobile,code);
        if(mobileCode == null){
            throw new CustomException(Code.ERROR,"验证码验证失败！");
        }
        commonMapper.updateMobileCode(mobileCode.getId());

        //绑定手机号临时解决方案临时解决方案
        UserBean user = userMapper.getUserByAccount(mobile);
        if(user != null){
            mobileCode = commonMapper.getMobileCode(mobile, user.getToken());
            commonMapper.addMobileCode(mobileCode);
        }


        return new RetBean(Code.SUCCESS,"验证成功",null);

    }


    /**
     *  检查验证码是否成功
     * @param mobile,code
     * @return
     */
    @RequestMapping("/common/checkMobileCodeGetToken.json")
    @ResponseBody
    public RetBean checkMobileCodeGetToken(HttpServletResponse response,HttpServletRequest request, String mobile,String code){

        if(StringUtils.isEmpty("mobile")){
            throw new CustomException(Code.ERROR,"电话号码不能为空");
        }
        if(StringUtils.isEmpty("code")){
            throw new CustomException(Code.ERROR,"验证码不能为空");
        }
        MobileCode mobileCode = commonMapper.getMobileCode(mobile,code);
        if(mobileCode == null){
            throw new CustomException(Code.ERROR,"验证码验证失败！");
        }
        commonMapper.updateMobileCode(mobileCode.getId());
        String token = UUID.randomUUID().toString();
        commonMapper.addMobileCode(new MobileCode(mobile,token));
        return new RetBean(Code.SUCCESS,"验证成功",token);

    }

    /**
     *  获取banner列表
     * @param
     * @return
     */
    @RequestMapping("/common/getBannerList.json")
    @ResponseBody
    public RetBean getBannerList(HttpServletResponse response,HttpServletRequest request){

        List<Banner> list = commonMapper.getBannerList();
        return new RetBean(Code.SUCCESS,"banner获取成功",list);

    }


    /**
     * 获取平台系统数据
     * @param request
     * @param response
     * @param token
     * @return
     */
    @RequestMapping("/common/sysDate.json")
    @ResponseBody
    public RetBean sysDate(HttpServletRequest request,HttpServletResponse response,String token){
        List<PlatformData> list = commonMapper.getSysDate();
        Date awardDate = null;
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("alltotalsalesA",commonMapper.getAlltotalsales("1")/10000);
        map.put("alltotalsalesB",commonMapper.getAlltotalsales("2")/10000);

        if(list.size()>0){
            awardDate = list.get(0).getCreatedate();
        }
        map.put("awardDate",awardDate);
        map.put("list",list);



        return new RetBean(Code.SUCCESS,"数据获取成功",map);
    }


    /**
     * 下单预留
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/common/addOrder.json")
    @ResponseBody
    public RetBean addOrder(HttpServletRequest request, HttpServletResponse response){
        List<PlatformData> lists = commonMapper.getSysDate();
        return new RetBean(Code.SUCCESS,"数据获取成功",lists);
    }



    /**
     *  检查验证码是否成功
     * @return
     */
    @RequestMapping("/common/getFunctionMenu.json")
    @ResponseBody
    public RetBean getFunctionMenu(HttpServletResponse response,HttpServletRequest request,String version){
        List<FunctionMenu> list = new ArrayList<FunctionMenu>();

        if(StringUtils.isEmpty(version)){
             version = "0";
        }

        System.out.println(commonMapper.getFunctionMenuCount(version));

        if(commonMapper.getFunctionMenuCount(version)<=0){
            list = commonMapper.getFunctionMenu();
        }


        return new RetBean(Code.SUCCESS,"获取成功",list);
    }

    /**
     *
     * @param response
     * @param request
     * @return
     */
    @RequestMapping("/common/getUpdateHttpsTimes.json")
    @ResponseBody
    public RetBean getUpdateHttpsTimes(HttpServletResponse response,HttpServletRequest request){
        return new RetBean(Code.SUCCESS,"获取成功",commonMapper.getUpdateHttpsTimes());
    }

}
